﻿using MvcApplication.CommonForm.Data.Mapping;
using MvcApplication.Entity.User;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace MvcApplication.CommonForm.Data
{
    public class CommonFormContext : DbContext
    {
         static CommonFormContext()
        {
            Database.SetInitializer<CommonFormContext>(null);
        }

        public CommonFormContext()
            : base("name=CommonFormContext")
        {
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = false;
        }

        public CommonFormContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
        }

        public CommonFormContext(string nameOrConnectionString, DbCompiledModel model)
            : base(nameOrConnectionString, model)
        {
        }

        public CommonFormContext(DbConnection existingConnection, bool contextOwnsConnection)
            : base(existingConnection, contextOwnsConnection)
        {
        }

        public CommonFormContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection)
            : base(existingConnection, model, contextOwnsConnection)
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //modelBuilder.Configurations.Add(new Post_Mapping());
            modelBuilder.Configurations.Add(new User_Mapping());
        }

        #region 文章
        public DbSet<User> User { get; set; }
        #endregion

        public override int SaveChanges()
        {
            try
            {
                return base.SaveChanges();
            }
            catch (DBConcurrencyException ex)
            {
                throw ex;
            }
            catch (DbUpdateException ex)
            {
                throw ex;
            }
        }
    }
}
